package com.meng.shiro;

import com.meng.b_service.UserService;
import com.meng.d_model.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 验证 （登录 或者 权限）
 *
 * @author MENG
 * @version 2018/1/23
 * @see
 */
public class CustomShiroRealm extends AuthorizingRealm
{
    /**
     * 用户service层
     */
    @Autowired
    private UserService userService;

    /**
     * 验证登录的
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
    {
        String email = (String)token.getPrincipal();

        User user = userService.getOne(email);

        if(user == null)
        {
            return null;
        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(

            user, //用户名

            user.getPassword(), //密码

            getName()  //realm name
        );

        return authenticationInfo;

    }

    /**
     * 验证权限的
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {
        return null;
    }
}
